const addCartEle = document.querySelector('.addCart')
const infoEle = document.querySelector('.info')

let str = localStorage.getItem('MOVECART')
let moveList = JSON.parse(str) || []
/* *----------------------------获取商品id--------------------- */
function getProductId() {
    //http://www.xiongmaoyouxuan.com/api/detail?id=104686496
    let url = location.href
    let params = url.substring(url.indexOf('?') + 1)
    let arr = params.split('=')
    let id = arr[1]
    return id
}
/*** ---------------根据id调用商品详情接口获取商品详情数据--------------- */
function getProductDetail() {
    let id = getProductId()
    ajaxPromise({
        method: 'get',
        url: 'http://localhost:8088/api/shop/list',
        data: {
            id
        }
    }).then(res => {
        let result = res.resultInfo.list
        console.log(result);
        console.log(result)
        onShow(result)
    }).catch(error => {
        console.log('详情页网络请求出错')
    })
}

/*--------------------------------数据渲染--------------------------*/
function onShow(detailList) {
    let newArr = detailList.filter(item => {
        return item.id == getProductId()
    })
    let arr = newArr.map(item => {
        return `  
            <div><img src="${item.picture}" alt="图片"></div>
            <div>
                <div>
                    <h6>${item.product}</h6>
                    <p class="fir"><span>月销112份</span><span>好评99%</span></p>
                </div>
                <div class="price">
                    <p class="sen"><span>￥${item.price}</span><span>￥${item.oldprice}</span></p>
                    <button>加入购物车</button>
                </div>
            </div>
               `
    }).join('')
    let inclArr = newArr.map(item => {
        return `        
               <h5>商品介绍</h5>
               <p>${item.detail}</p>
               `

    }).join('')

    addCartEle.innerHTML = arr
    infoEle.innerHTML = inclArr

    let addBtn = document.querySelector('.price button')
    addBtn.addEventListener('click', function () {
        newArr.forEach(item => {
            let id = item.id
            let title = item.product
            let price = item.price
            let img = item.picture
            let totalPrice = 0
            let obj = {
                id,
                title,
                price,
                num: 1,
                img,
                totalPrice: price
            }
            console.log(obj);
            if (moveList.length > 0) {
                let reaptList = moveList.some(item => item.id == id)
                //！！隐式转换一定为布尔值
                if (!!reaptList) {
                    moveList.forEach(item => {
                        if (item.id == id) {
                            item.num = item.num + 1
                        }
                    })
                } else {
                    moveList.push(obj)
                }
            } else {
                moveList.push(obj)
            }
            localStorage.setItem('MOVECART', JSON.stringify(moveList))
            if (confirm('加入购物车成功，是否要调转去购物车？')) {
                location.href = 'cart.html'
            }
        })
    })
}
/**------------------------跳转到购物车-----------------------------------*/
function onCart(newArr) {
    let addBtn = document.querySelector('.price button')
    addBtn.addEventListener('click', function () {
        let title = newArr.product
        let price = newArr.price
        let img = newArr.picture
        let singlePrice = 0
        let obj = {
            title,
            price,
            num: 1,
            img
        }
        console.log(obj);
        if (list.length > 0) {
            let reaptList = list.some(item => item.id == id)
            //！！隐式转换一定为布尔值
            if (!!reaptList) {
                list.forEach(item => {
                    if (item.id == id) {
                        item.num = item.num + 1
                    }
                })
            } else {
                list.push(obj)
            }
        } else {
            list.push(obj)
        }
        localStorage.setItem('MOVECART', JSON.stringify(list))
        // if (confirm('加入购物车成功，是否要调转去购物车？')) {
        //     location.href = 'cart.html'
        // }

    })
}
getProductDetail()